package com.siao.component
{
	import com.siao.display.SingleElement;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 四态按钮
	 * @author SiaoLeon
	 */
	public class StateButton extends SingleElement
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		/**
		 * 按钮是否可用
		 */
		public function get enabled():Boolean
		{
			return this.buttonMode;
		}
		
		public function set enabled(value:Boolean):void
		{
			if (value)
			{
				this.onNormal();
			}
			else
			{
				this.onDisabled();
				
				if (this.stage != null)
				{
					this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
					this.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeaveHandler);
				}
				this.removeEventListener(MouseEvent.ROLL_OUT, onMouseOutHandler);
				this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
				this.removeEventListener(MouseEvent.CLICK, onClickHandler);
			}
			
			this.buttonMode = value;
			this.mouseEnabled = value;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var isMouseOver:Boolean;
		
		private var isMouseDown:Boolean;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function StateButton()
		{
			super();
			
			this.mouseChildren = false;
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		override protected final function init():void
		{
			super.init();
			
			this.updateState();
			
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler, false, 0, true);
		}
		
		override protected final function exit():void
		{
			super.exit();
			
			this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
			
			if (this.stage != null)
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
				this.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeaveHandler);
			}
		}
		
		/**
		 * 普通状态时处理函数
		 */
		protected function onNormal():void
		{
		}
		
		/**
		 * 鼠标经过状态时处理函数
		 */
		protected function onMouseOver():void
		{
		}
		
		/**
		 * 鼠标左键按下状态时处理函数
		 */
		protected function onMouseDown():void
		{
		}
		
		/**
		 * 鼠标点击处理时处理函数
		 */
		protected function onMouseClick():void
		{
		}
		
		/**
		 * 不可用状态时处理函数
		 */
		protected function onDisabled():void
		{
		}
		
		protected function updateState():void
		{
			if (!this.enabled)
			{
				this.onDisabled();
				return;
			}
			
			if (this.isMouseDown)
			{
				this.onMouseDown();
				return;
			}
			
			if (this.isMouseOver)
			{
				this.onMouseOver();
				return;
			}
			
			this.onNormal();
		}
		
		/**
		 * 鼠标划过时
		 * @param event
		 */
		private function onMouseOverHandler(event:MouseEvent):void
		{
			if (!this.enabled)
			{
				return;
			}
			
			this.isMouseOver = true;
			
			this.updateState();
			
			this.addEventListener(MouseEvent.ROLL_OUT, onMouseOutHandler, false, 0, true);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler, false, 0, true);
			this.addEventListener(MouseEvent.CLICK, onClickHandler, false, 0, true);
			this.stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeaveHandler, false, 0, true);
		}
		
		/**
		 * 鼠标划出时
		 * @param event
		 */
		private function onMouseOutHandler(event:MouseEvent):void
		{
			this.isMouseOver = false;
			
			this.onNormal();
			
			if (!this.isMouseDown && this.stage != null)
			{
				this.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeaveHandler);
			}
			this.removeEventListener(MouseEvent.ROLL_OUT, onMouseOutHandler);
			this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			this.removeEventListener(MouseEvent.CLICK, onClickHandler);
		}
		
		/**
		 * 鼠标离开舞台时
		 * @param event
		 */
		private function onMouseLeaveHandler(event:Event):void
		{
			this.isMouseOver = false;
			this.isMouseDown = false;
			
			this.onNormal();
			
			this.removeEventListener(MouseEvent.ROLL_OUT, onMouseOutHandler);
			this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			this.removeEventListener(MouseEvent.CLICK, onClickHandler);
			
			if (this.stage != null)
			{
				this.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeaveHandler);
			}
		}
		
		/**
		 * 鼠标按下时
		 * @param event
		 */
		private function onMouseDownHandler(event:MouseEvent):void
		{
			this.isMouseDown = true;
			
			this.onMouseDown();
			
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler, false, 0, true);
		}
		
		/**
		 * 鼠标抬起时
		 * @param event
		 */
		private function onMouseUpHandler(event:MouseEvent):void
		{
			this.isMouseDown = false;
			
			this.onNormal();
			
			if (this.stage != null)
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			}
		}
		
		/**
		 * 鼠标点击时
		 * @param event
		 */
		private function onClickHandler(event:MouseEvent):void
		{
			if (this.enabled)
			{
				this.onMouseClick();
				this.onMouseOver();
			}
		}
	
	}
}
